package com.iamback.dy.task.taskjob;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List; 
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.iamback.dy.config.redis.RedisTemplateUtil;
import com.iamback.dy.entity.CommentExuser;
import com.iamback.dy.entity.DyContentComment;
import com.iamback.dy.entity.DyMedia;
import com.iamback.dy.entity.DyuserBlocked;
import com.iamback.dy.mapper.CommentExuserMapper;
import com.iamback.dy.mapper.DyContentCommentMapper;
import com.iamback.dy.mapper.DyMediaMapper;
import com.iamback.dy.mapper.DyuserBlockedMapper;

@Component
@Configuration
public class ContentCommentTask {
  
	@Autowired
	private DyContentCommentMapper contentCommentMapper;
	@Autowired
	private CommentExuserMapper commentExuserMapper;
	@Autowired
	private DyMediaMapper dyMediaService;
	@Autowired
	private RedisTemplateUtil redisTemplateUtil;
	@Autowired
	private DyuserBlockedMapper dyuserBlockedService;
	
	
	@Scheduled(fixedRate = 300000)
	public void synContentComment(){
//		redisTemplateUtil.del("examine:comment:list:last");
		String idv = (String) redisTemplateUtil.get("examine:comment:list:last");
		if(idv==null||idv.equals("")){
			Map<String, Object> map =new HashMap<String, Object>();
			map.put("numPerPage", 1);
			List<CommentExuser> pageExuser = commentExuserMapper.findCommentExuserBy(map);
			if(!pageExuser.isEmpty()){
				idv=String.valueOf(pageExuser.get(0).getCommentId());
			}else{
				idv = "0";
			}
		}
		List<DyContentComment> comments= contentCommentMapper.findDyContentComment(Long.valueOf(idv));
		if(!comments.isEmpty()){
			List<CommentExuser> commentExusers =new ArrayList<CommentExuser>();
			String idValue=""; 
			for (DyContentComment contentComment: comments) {
				CommentExuser commentExuser =new CommentExuser();
				commentExuser.setCommentContent(contentComment.getCommentContent());
				commentExuser.setCommentDyId(contentComment.getCommentDyId());
				commentExuser.setCommentId(contentComment.getCommentId());
				commentExuser.setCommentType(contentComment.getCommentType());
				commentExuser.setDyContentId(contentComment.getDyContentId());
				commentExuser.setMediaIds(contentComment.getMediaIds()); 
				commentExuser.setReplyToCommentId(contentComment.getReplyToCommentId());
				commentExuser.setReplyToDyId(contentComment.getReplyToDyId());
				commentExuser.setType(contentComment.getType());
				commentExuser.setUploadDate(contentComment.getUploadDate());
				if(contentComment.getMediaIds()!=null&&!contentComment.getMediaIds().trim().equals("")){
					List<DyMedia> mediaList =dyMediaService.findDyMediaByIn(copyMedia(contentComment.getMediaIds())) ;
					commentExuser.setMediaJson(JSON.toJSONString(mediaList));
				}
				Map<String, Object> map =new HashMap<String, Object>();
				map.put("dyId", contentComment.getCommentDyId());
				map.put("numPerPage", 1);
				List<DyuserBlocked> page2=dyuserBlockedService.findDyuserBlocked(map);
				if(!page2.isEmpty()){
					Date date =new Date();
					if(!page2.get(0).getBlockedType().equals("0")){//非正常 用戶  
						if(date.before(page2.get(0).getBlockedEndtime())||page2.get(0).getBlockedType().equals("4")){
							commentExuser.setExResult("3");
							commentExuser.setExDesc("系统导入自动处理为 不通过");
							//禁言下 自动设置 评论为不通过
							contentComment.setCommentType(3L);
							contentCommentMapper.updateContentComment(contentComment);
						}else{
							commentExuser.setExResult("0");
						}
					}else{
						commentExuser.setExResult("0");
					} 
				}else{
					commentExuser.setExResult("0");
				}
				if(commentExuser.getExDesc()==null||commentExuser.getExDesc().equals("")){
					commentExuser.setExDesc("");
				}
				commentExuser.setInputTime(new Date());
				commentExusers.add(commentExuser);
				if(idValue.equals("")){
				   idValue=String.valueOf(contentComment.getCommentId());
				}
			}
			redisTemplateUtil.set("examine:comment:list:last", idValue,600000);
			commentExuserMapper.insertAll(commentExusers);
		}
	}
	
	
	@Scheduled(fixedRate = 2040000)
	public void clearCommentTask(){
		Calendar nowAfter = Calendar.getInstance();
		nowAfter.add(Calendar.MINUTE, -30);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("allotTime", nowAfter.getTime());
		map.put("exResult", "0");
		List<CommentExuser>  commentExusers =commentExuserMapper.findCommentExuserOverdue(map);
		if(!commentExusers.isEmpty()&&commentExusers.size()>0){
			for(CommentExuser contentComment:commentExusers){
				CommentExuser commentExuserEx=commentExuserMapper.findCommentExuserById(contentComment.getId());
				if(commentExuserEx.getExResult().equals("0")){
					contentComment.setExResult("0");
					contentComment.setAllotTime(null);
					contentComment.setExuserId(null);
					commentExuserMapper.updateCommentExuserBysyn(contentComment);
				}
			}
		}
	}
	 
	private static List<Long> copyMedia(String mediaIds){
		List<Long> idList = new ArrayList<Long>();
		String[] ids= mediaIds.split(",");
		for(String id :ids){
			Long v = Long.valueOf(id);
			idList.add(v);
		}
		return idList;
		
	}
} 
	 
